{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
}}

<TabPageHeader
  @model={{@backend}}
  @filterRoles={{not @promptConfig}}
  @query={{this.query}}
  @breadcrumbs={{@breadcrumbs}}
  @handleSearch={{this.handleSearch}}
  @handleInput={{this.handleInput}}
  @handleKeyDown={{this.handleKeyDown}}
>
  {{#unless @promptConfig}}
    <ToolbarLink @route="roles.create" @type="add" data-test-toolbar-roles-action>
      Create role
    </ToolbarLink>
  {{/unless}}
</TabPageHeader>

{{#if @promptConfig}}
  <ConfigCta />
{{else if (not @roles)}}
  {{#if @filterValue}}
    <EmptyState @title="There are no roles matching &quot;{{@filterValue}}&quot;" />
  {{else}}
    <EmptyState
      data-test-config-cta
      @title="No roles yet"
      @message="When created, roles will be listed here. Create a role to start generating service account tokens."
    />
  {{/if}}
{{else}}
  <div class="has-bottom-margin-s">
    {{#each @roles as |role|}}
      <ListItem @linkPrefix={{this.mountPoint}} @linkParams={{array "roles.role.details" role.name}} as |Item|>
        <Item.content>
          <Icon @name="user" />
          <span data-test-role={{role.name}}>{{role.name}}</span>
        </Item.content>
        <Item.menu>
          {{#if (or role.canRead role.canEdit role.canDelete)}}
            <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
              <dd.ToggleIcon
                @icon="more-horizontal"
                @text="More options"
                @hasChevron={{false}}
                data-test-popup-menu-trigger
              />
              {{#if role.canRead}}
                <dd.Interactive @route="roles.role.details" @model={{role}} data-test-details>Details</dd.Interactive>
              {{/if}}
              {{#if role.canEdit}}
                <dd.Interactive data-test-edit @route="roles.role.edit" @model={{role}}>Edit</dd.Interactive>
              {{/if}}
              {{#if role.canDelete}}
                <dd.Interactive
                  data-test-delete
                  @color="critical"
                  {{on "click" (fn (mut this.roleToDelete) role)}}
                >Delete</dd.Interactive>
              {{/if}}
            </Hds::Dropdown>
          {{/if}}
        </Item.menu>
      </ListItem>
    {{/each}}
    {{#if this.roleToDelete}}
      <ConfirmModal
        @color="critical"
        @confirmMessage="Deleting this role means that you’ll need to recreate it in order to generate credentials again."
        @onConfirm={{fn this.onDelete this.roleToDelete}}
        @onClose={{fn (mut this.roleToDelete) null}}
      />
    {{/if}}
  </div>
{{/if}}